<?php

abstract class Quad_Router_Abstract implements Quad_Router_Interface, Quad_MVC_Interface {
	
	protected $_bootstrap;
	
	protected $_controllerName;
	
	protected $_actionName;
  
  public function __construct(Quad_Bootstrap_Interface $bootstrap) {
    $this->_bootstrap = $bootstrap;
  }	
	
  public function forward($controller, $action, $moreParams = array()) {
    $_GET = array_merge($_GET, $moreParams);
    $_REQUEST = array_merge($_GET, $_POST, $_COOKIE);
    $this->_controllerName = $controller;
    $this->_actionName = $action;
		// TODO: reboot instead of throwing   
    throw new Quad_FrontController_Forward();
  }	
  
  public function getControllerName() {
    return $this->_controllerName; 
  }
  
  public function getActionName() {
    return $this->_actionName;
  }
  
  public function getControllerClass() {
    return ucwords("{$this->_controllerName}Controller");
  }
  
  public function getActionMethod() {
    return "{$this->_actionName}Action";  	
  }
  
  public function requireControllerClass() {
// TODO: make configurable
    $controllerClass = $this->getControllerClass();
require_once("d:/wamp/www/quad/controllers/$controllerClass.php");  	
  }
	
	public function getBootstrap() {
		return $this->_bootstrap;
	}
	  
  public function getFrontController() {
      if ($this->_bootstrap->frontController instanceof Quad_FrontController_Interface) {
      return $this->_bootstrap->frontController;
    } else {
      $class = get_class($this->_bootstrap);
      throw new Quad_Exception("Property 'frontController' in Bootstrap class '$class' must implement Quad_FrontController_Interface");
    }  	
  }
  
  public function getController() {
    if ($this->getFrontController()->controller instanceof Quad_Controller_Interface) {
      return $this->getFrontController()->controller;
      
    } else {
      $class = get_class($this->getFrontController());
      throw new Quad_Exception("Property 'controller' in FrontController class '$class' must implement Quad_Controller_Interface");
    }   
  }	
	
	public function getRouter() {
		return $this;
	}
	
	public function getUrl() {
		return $this->getBootstrap()->getUrl();
	}
	
}